import 'dart:async';
import 'dart:ui';

import 'package:bluetooth_flutter_plugin/src/bluetooth_helper/bluetooth_adapter_base.dart';
import 'package:bluetooth_flutter_plugin/src/bluetooth_helper/bluetooth_connect_state.dart';
import 'package:bluetooth_flutter_plugin/src/bluetooth_helper/bluetooth_manager.dart';
import 'package:bluetooth_flutter_plugin/src/bluetooth_helper/bluetooth_interceptor.dart';
import 'package:bluetooth_flutter_plugin/src/bluetooth_helper/bluetooth_send_handler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

/// 蓝牙助手类
///
/// 该类提供了一系列方法来管理蓝牙设备的连接、扫描和数据传输。
///
/// T 是一个泛型参数，必须继承自 [BluetoothAdapterBase]。
///
/// 属性:
/// - `bluetoothAdapter`: 蓝牙适配器实例。
/// - `bluetoothConnectState`: 获取蓝牙连接状态的流控制器。
/// - `connectedDevice`: 获取当前已连接的蓝牙设备。
///
/// 方法:
/// - `search`: 搜索目标蓝牙设备。
/// - `startScan`: 开始扫描蓝牙设备。
/// - `connect`: 连接到指定的蓝牙设备。
/// - `reqAndRsp`: 发送请求数据并接收响应。
/// - `stopScan`: 停止扫描蓝牙设备。
/// - `disconnect`: 断开当前连接的蓝牙设备。

class BluetoothHelper<T extends BluetoothAdapterBase,
    I extends BluetoothInterceptor, S extends BluetoothSendHandler> {
  // 蓝牙适配器
  final T bluetoothAdapter;

  // 蓝牙发送处理器
  final S bluetoothSendHandler;

  // 蓝牙请求响应拦截器
  final I? bluetoothInterceptor;

  // 构造函数
  BluetoothHelper(
    this.bluetoothAdapter,
    this.bluetoothSendHandler, {
    this.bluetoothInterceptor,
  });

  // 获取蓝牙连接状态
  Stream<BluetoothConnectState> get bluetoothConnectState =>
      _manager.bluetoothConnectState;

  // 获取已连接设备
  BluetoothDevice? get connectedDevice => _manager.connectedDevice;

  // 获取蓝牙管理器
  final BluetoothManager _manager = BluetoothManager();

  // 搜索【目标蓝牙设备】
  Future<bool> search(
    BluetoothDevice bluetoothDevice, {
    Duration? timeout,
  }) async {
    return _manager.search(bluetoothDevice, timeout: timeout);
  }

  // 开始扫描
  Future<void> startScan({
    Duration? timeout,
    VoidCallback? onTimeout,
    List<Guid>? serviceUuids,
    CallbackScanResults? callbackScanResults,
    bool filterDuplicate = true,
  }) {
    return _manager.startScan(
      timeout: timeout,
      onTimeout: onTimeout,
      serviceUuids: serviceUuids,
      callbackScanResults: callbackScanResults,
      filterDuplicate: filterDuplicate,
    );
  }

  // 连接蓝牙设备
  Future<void> connect(BluetoothDevice device, {Duration? timeout}) async {
    clearWaitingRequests();
    await _manager.connectDevice(device, bluetoothAdapter, timeout: timeout);
  }

  // 发送请求并接收响应
  Future<void> sendRequest(
    List<int> requestData, {
    bool withoutResponse = false,
  }) {
    return _manager.write(
      bluetoothSendHandler,
      requestData,
      withoutResponse: withoutResponse,
      bluetoothInterceptor: bluetoothInterceptor,
    );
  }

  // 清空等待请求
  void clearWaitingRequests() {
    bluetoothSendHandler.requestQueue.clear();
    bluetoothSendHandler.responseStreamMap.clear();
  }

  // 停止扫描
  Future<void> stopScan() => _manager.stopScan();

  // 断开蓝牙设备
  Future<void> disconnect() => _manager.disconnect();
}
